home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_300 / 352_01 / vlist.cpp < prev    next >
C/C++ Source or Header  |  1991-04-28  |  2KB  |  95 lines

  1. // VLIST.CPP
  2. //
  3. //    defines class Vlist which is a variable list of ptrs.
  4. //        ptrs to strings or other objects may be added to the list or removed
  5. //            (operates like a stack)
  6. //        The list can also be accessed like an array of ptrs, with NULL at end.
  7. //
  8. //    NOTE: when you place an object in Vlist, the list makes a copy of the object
  9. //            you may remove the object by calling  list.remove(ptr);
  10. //            This frees the lists copy. 
  11. //
  12. //            If you place a heap object on the list,
  13. //                you are still responsible for cleaning up your copy.
  14. //            This is diferent from container classes in TurboC++ classlib 
  15. //
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include <alloc.h>
  19.  
  20.  
  21.  
  22. #include "wtwg.h"        /* needed for wmalloc() */
  23.  
  24.  
  25. #include "dblib.h"
  26.  
  27.  
  28.         // define number of ptrs to allocate for each time.    
  29.         #define VSTEP 25        
  30.         
  31.         Vlist::Vlist (void)
  32.             {
  33.             max = n = 0;
  34.             list = NULL;
  35.             }        
  36.             
  37.         void Vlist::clear (void)
  38.             {
  39.             int vn=n; 
  40.             void **vl=list;
  41.             for ( int i=0; i<vn; ++i )
  42.                 {
  43.                 free (vl[i]);
  44.                 }
  45.             n=0;
  46.             if ( max > 0 ) { max=0; free (vl); list =NULL; }
  47.             }
  48.         
  49.         int Vlist::compare ( void* a, int bn )
  50.             {
  51.             return  ( String::caseSens ? strcmp  ( (char*)a,(char*)(list[bn])) 
  52.                                        : stricmp ( (char*)a,(char*)(list[bn]))
  53.                                        );
  54.             }
  55.         void  Vlist::copy ( void *a )
  56.             {
  57.             
  58.             int len = (a==NULL) ? 0 : strlen ( (char*)a );
  59.             void *ptr = wmalloc ( 1+len, "Vlist" );
  60.             if ( len > 0 )  memcpy ( ptr, a, len );
  61.             ((char*)ptr)[len]=0;
  62.  
  63.             void **vl =list;
  64.             register int  vn=n;
  65.             vl[vn] = ptr;
  66.             vl[++vn] = NULL;    
  67.             n = vn;
  68.             }
  69.         
  70.         
  71.         void Vlist::push ( void* data )        
  72.             {
  73.             int vn = n;
  74.             int vmax =max;
  75.             
  76.             if ( vn == vmax )
  77.                 {
  78.                 // need to reallocate.
  79.                 max += VSTEP;
  80.                 vmax = max;
  81.                 // NOTE: must alloc enough mem for list and for terminal NULL.
  82.                 list = (void**)wrealloc (list,(vmax+1)*sizeof(void*), "Vlist");
  83.                 list[vn] = NULL;
  84.                 }    // end of reallocation.
  85.                 
  86.             Vlist::copy ( data );
  87.             
  88.             return;        /* Vlist::push() */
  89.             }
  90.         
  91.             
  92.  
  93. ///////////////// end of VLIST.CPP
  94.  
  95.